package org.gap.jseed.apf;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.tools.Diagnostic.Kind;

public class MessageWriter {
	private static final int TEXT_WIDTH_LIMIT = 40;
	private final ProcessingEnvironment processingEnv;
	
	public MessageWriter(ProcessingEnvironment processingEnv) {
		this.processingEnv = processingEnv;
	}
	
	public void warningMessage(String message, ExecutableElement exec) {
		processingEnv.getMessager().printMessage(
				Kind.WARNING, 
				formatHoverText(Messages.bind(message, exec.getSimpleName())),
				exec);
	}

	private String formatHoverText(String text) {
		int nextSpace = 0;
		int indicesFound = 1;
		while (nextSpace < text.length() && indicesFound * TEXT_WIDTH_LIMIT < text.length()) {
			nextSpace = text.indexOf(" ", nextSpace);
			if (nextSpace == -1) {
				break;
			}
			if (nextSpace > indicesFound * TEXT_WIDTH_LIMIT + indicesFound) {
				text = text.substring(0, nextSpace) + "\n" + text.substring(nextSpace + 1);
				indicesFound++;
			}
			nextSpace++;
		}
		return text;
	}
}
